package top.lhj.security.example.controller;

import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import top.lhj.security.example.service.UserService;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author liHaoJie
 * @Create 2024/1/31 19:34
 * @Version 1.0
 */
@RestController
@Slf4j
public class IndexController {
    
    @GetMapping("/")
    public Map<String, Object> index() {
        //获取SecurityContext
        SecurityContext context = SecurityContextHolder.getContext();
        Authentication authentication = context.getAuthentication();
        //用户认证信息
        Object principal = authentication.getPrincipal();
        //用户的凭证信息
        Object credentials = authentication.getCredentials();
        //用户权限
        Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
        String username = authentication.getName();
        
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("data", username);
        return map;
    }
    
}
